<!--suppress ALL -->
<div class="layui-fluid layui-anim website-anim" id="website-topic-select" lay-title="课题征集管理"
     xmlns:th="http://www.w3.org/1999/xhtml">
    <div class="layui-row website-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body website-table-full">
                    <form class="layui-form layui-table-form" lay-filter="topic-select-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">课题名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="topicName" autocomplete="off" class="layui-input"
                                                   placeholder="请输入标题">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">年份</label>
                                        <div class="layui-input-inline">
                                            <input type="text" class="layui-input" id="topicYearSearch" readonly
                                                   name="year">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-blue-plain table-action"
                                     id="query">
                                    查询
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain table-action"
                                     id="reset">
                                    重置
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-blue-plain table-action"
                                     id="toChanges">
                                    转课题
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain table-action"
                                     id="export">
                                    导出
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="topicSelectTable" lay-data="{id: 'topicSelectTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<!--表格中字典的显示-->
<script type="text/html" id="subject_type">
    {{#
    var dictList=layui.dict.getByCodes(d.subjectTypeCodes);
    }}
    <span>
    {{# layui.each(dictList,function(index,item) { }}
    {{ item.name }}&nbsp;
    {{#  }); }}
    </span>
</script>

<!--表格中的操作栏-->
<script type="text/html" id="topic-select-option">
    <span shiro:lacksPermission="topicSelect:list,topicSelect:update,topicSelect:delete">
        <span class="layui-badge-dot website-bg-orange"></span> 无权限
    </span>
    <a lay-event="detail" class="website-green" shiro:hasPermission="topicSelect:list">详情</a>
    <a lay-event="changeTopic" class="{{ d.isChange?'website-red':'website-blue'}}"
       shiro:hasPermission="topicSelect:list">转课题</a>
</script>

<script data-th-inline="none" type="text/javascript">
    layui.use(['jquery', 'form', 'table', 'website', 'dict', 'laydate'], function () {
        var $ = layui.jquery,
            website = layui.website,
            form = layui.form,
            table = layui.table,
            dict = layui.dict,
            dropdown = layui.dropdown,
            laydate = layui.laydate,
            $view = $('#website-topic-select'),
            $export = $view.find('#export'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $toChanges = $view.find('#toChanges'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'createTime', type: null},
            tableIns,
            createTimeFrom,
            createTimeTo;
        form.render();
        initTable();
        //初始化年份的搜索框
        let topicPlanYearSearch = laydate.render({
            elem: '#topicYearSearch', //指定元素
            type: 'year',
            trigger: 'click',
            value: new Date()
        });


        /**
         * table操作栏的按钮监听事件
         **/
        table.on('tool(topicSelectTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'detail') {
                website.modal.view('课题征集', 'biz/topicSelect/topicSelectDetail/' + data.selectId, {
                    area: $(window).width() <= 750 ? '95%' : '50%'
                });
            }
            if (layEvent === 'changeTopic') {
                changeTopic(data.selectId);
            }
        });

        /**
         * 表格的排序点击
         **/
        table.on('sort(topicSelectTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            //初始化年份的搜索框
            let topicPlanYearSearch = laydate.render({
                elem: '#topicYearSearch', //指定元素
                type: 'year',
                trigger: 'click',
                value: new Date()
            });

            sortObject.type = 'null';
            createTimeTo = null;
            createTimeFrom = null;
            tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
        });

        $export.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            if (!params.year) {
                website.alert.warn('请选择需导出数据的年份');
                return;
            }
            website.download(ctx + 'topicSelect/export', params, "中国法学会"+params.year+"年度部级法学研究课题建议征集汇总表.xlsx");
        });

        $toChanges.on('click', function () {
            var checkStatus = table.checkStatus('topicSelectTable');
            if (!checkStatus.data.length) {
                website.alert.warn('请选择需要转换的数据');
            } else {
                var selectIds = [];
                layui.each(checkStatus.data, function (key, item) {
                    selectIds.push(item.selectId)
                });
                changeTopic(selectIds.join(','));
            }
        });

        /**
         * 改变课题方法
         */
        function changeTopic(ids) {
            website.modal.open('请填写转换信息', 'biz/topicSelect/changeTopics/' + ids, {
                btn: ['提交', '取消'],
                area: $(window).width() <= 750 ? '95%' : '50%',
                offset: '30px',
                yes: function (index, layero) {
                    $('#change-topic').find('#submit').trigger('click');
                }
            });
        }

        /**
         * 点击行选中复选框
         **/
        $('body').on("click", ".layui-table-body table.layui-table tbody tr td", function () {
            if ($(this).attr("data-field") === "0") return;
            $(this).siblings().eq(0).find('i').click();
        });


        /**
         * 初始化表格
         */
        function initTable() {
            tableIns = website.table.init({
                    elem: $view.find('table'),
                    id: 'topicSelectTable',
                    url: ctx + 'biz/topicSelect/list',
                    where: {year: new Date().getFullYear()},
                    cols: [[
                        {type: 'checkbox'},
                        /* { type: 'numbers', title: '序号', },*/
                        {field: 'year', title: '征集年份', minWidth: 100},
                        {field: 'topicName', title: '课题名称', minWidth: 100},
                        {title: '学科分类', templet: '#subject_type'},
                        {field: 'unitName', title: '单位名称', minWidth: 100},
                        {field: 'userName', title: '建议人姓名', minWidth: 100},
                        {field: 'mobile', title: '手机号', minWidth: 100},
                        {field: 'phone', title: '座机', minWidth: 100},
                        {field: 'professionCode', title: '职称', minWidth: 100},
                        {
                            title: '职称级别', minWidth: 100, templet: function (d) {
                                var dict = layui.dict.getByCode(d.academic);
                                if (dict) {
                                    return dict.name
                                }
                                return '';
                            }
                        },
                        {
                            title: '职务', minWidth: 100, templet: function (d) {
                                var dict = layui.dict.getByCode(d.position);
                                if (dict) {
                                    return dict.name
                                }
                                return '';
                            }
                        },
                        {title: '操作', toolbar: '#topic-select-option', minWidth: 140}
                    ]]
                }
            );
        }

        /**
         * 获取查询条件
         * @returns {{}}
         */
        function getQueryParams() {
            var params = $searchForm.serializeJson();
            return params;
        }

    })
</script>